﻿/**********************************************************************************
 * Description:     
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2008-8-5     New           DuYaoHui     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Common;
namespace Fjaspx.UI.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Administrator_class_index : AdminPage
    {
        Fjaspx.Sys.Model.ROYcms_class model = new Fjaspx.Sys.Model.ROYcms_class();

        #region ClassKind属性
        /// <summary>
        /// Gets or sets the kind of the class.
        /// </summary>
        /// <value>The kind of the class.</value>
        protected int ClassKind
        {
            get
            {
                return (int)ViewState["ClassKind"];
            }
            set
            {
                ViewState["ClassKind"] = value;
            }
        }
        #endregion

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SystemCms.AdminSesion();
                this.ClassKind = Convert.ToInt32(Request.QueryString["ClassKind"] == null ? "10" : Request.QueryString["ClassKind"]);
                BindData();
            }
        }


        #region 数据绑定
        private void BindData()
        {

            DataSet ds = new Fjaspx.Sys.Bll.ROYcms_class().GetClassList(this.ClassKind);
            this.rptMenuList.DataSource = ds.Tables[0].DefaultView;
            this.rptMenuList.DataBind();

        }
        #endregion

        #region 显示数据处理
        /// <summary>
        /// Handles the ItemDataBound event of the rptMenuList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
        protected void rptMenuList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
               // Literal Literal_id = (Literal)e.Item.FindControl("Literal_id");
                //HyperLink LabClassNm = (HyperLink)e.Item.FindControl("LabClassNm");
                Label LabClassNm = (Label)e.Item.FindControl("LabClassNm");

                string LitStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";

                string LitImg1 = "<img src=/administrator/images/jia.gif ><img src=/administrator/images/openfolder.gif align=absmiddle />";
                string LitImg2 = "<img src=/administrator/images/page_white_star.png align=absmiddle />";
                string LitImg3 = "<img src=/administrator/images/t.gif align=absmiddle />";


                DataRowView drv = (DataRowView)e.Item.DataItem;
                int ClassTj = Convert.ToInt32(drv["ClassTj"]);

                if (ClassTj == 1)
                {
                    LabClassNm.Font.Bold = true;
                    LitFirst.Text = LitImg1;
                    //Literal_id.Text = drv["ClassList"].ToString().Replace(",","");
                   
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, ClassTj * 20, LitImg3, LitImg2);
                    //Literal_id.Text = drv["ClassList"].ToString().Substring(0, drv["ClassList"].ToString().IndexOf(",")) + "_sub";

                }
            }
        }
        #endregion

        #region 编辑删除处理
        /// <summary>
        /// Handles the ItemCommand event of the rptMenuList control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void rptMenuList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField txtClassId = (HiddenField)e.Item.FindControl("txtClassId");
            //HyperLink LabClassNm = (HyperLink)e.Item.FindControl("LabClassNm");
            Label LabClassNm = (Label)e.Item.FindControl("LabClassNm");
            switch (e.CommandName.ToLower())
            {
                //case "btnedit":
                //    this.txtClassName.Text = LabClassNm.Text.Trim();
                //    this.HidClassId.Value = txtClassId.Value.Trim();
                //    this.DdlMenu.SelectedValue = new Fjaspx.Sys.Bll.ROYcms_class().GetPreClassId(txtClassId.Value.Trim()).Trim();
                //    this.btnAdd.Visible = false;
                //    this.btnEdit.Visible = true;
                //    break;

                case "btndelete":
                   
                    if (new Fjaspx.Sys.Bll.ROYcms_class().DelByClassId(txtClassId.Value))
                    {
                        // ___ROYcms_news_Bll.Delete("classname="+Convert.ToInt32( ___ROYcms_class_Bll._GetField(txtClassId.Value,"Id")));
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('栏目删除成功！')", true);
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('删除操作失败，请与管理员联系！')", true);
                    }
                    BindData();
                    break;
            }
        }
        #endregion

        #region 列表顺序保存操作
        /// <summary>
        /// 列表顺序保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            int UpSum = 0;
            bool ReVal = true;
            foreach (RepeaterItem RptItem in this.rptMenuList.Items)
            {
                HiddenField txtClassId = (HiddenField)RptItem.FindControl("txtClassId");
                TextBox txtOrder = (TextBox)RptItem.FindControl("txtOrder");
                try
                {
                    ReVal = new Fjaspx.Sys.Bll.ROYcms_class().UpdateClassOrder(txtClassId.Value.Trim(), Convert.ToInt32(txtOrder.Text));
                }
                catch
                {
                    ReVal = false;
                    Fjaspx.Common.SystemCms.InsertErrLog("列表操作排序错误！", Request.PhysicalPath);
                }

                if (ReVal == false)
                {
                    ++UpSum;
                }
            }
            if (UpSum == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('所有设定的栏目顺序都已保存成功！')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", string.Format("alert('栏目顺序部份保存成功,共有{0}条记录保存失败!')", UpSum), true);
            }
           BindData();
        }
        #endregion

        protected void Btn_add_Click(object sender, EventArgs e)
        {
            Response.Redirect("add.aspx?ClassKind="+this.ClassKind);
        }


    }
}